package com.joyoung.common.utils;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
 * FragmentUtil
 *
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public final class FragmentUtil {

    private FragmentUtil() {}

    public static void replace(FragmentManager fragmentManager, @IdRes int id, Fragment fragment) {
        replace(fragmentManager, id, fragment, null);
    }

    public static void replace(FragmentManager fragmentManager, @IdRes int id, Fragment fragment, String tag) {
        if (fragmentManager == null) {
            return;
        }
        fragmentManager.beginTransaction().replace(id, fragment, tag).commitAllowingStateLoss();
    }

    public static void replace(FragmentActivity activity, @IdRes int id, Fragment fragment) {
        replace(activity, id, fragment, null);
    }

    public static void replace(FragmentActivity activity, @IdRes int id, Fragment fragment, String tag) {
        if (activity == null || activity.getFragmentManager() == null) {
            return;
        }
        activity.getSupportFragmentManager().beginTransaction().replace(id, fragment, tag).commitAllowingStateLoss();
    }

    public static void replace(Fragment parent, @IdRes int id, Fragment fragment) {
        replace(parent, id, fragment, null);
    }

    public static void replace(Fragment parent, @IdRes int id, Fragment fragment, String tag) {
        if (parent == null) {
            return;
        }
        parent.getChildFragmentManager().beginTransaction().replace(id, fragment, tag).commitAllowingStateLoss();
    }
}
